package com.controller;


import com.pojo.Category;
import com.pojo.ResponseDto;
import com.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.List;

@RequestMapping("/category")
@Controller
public class CategoryController {
    @Autowired
    private CategoryService categoryservice;


    @RequestMapping("/category_show")
    public String category_show(){
        return "category_add";

    }

    /*
    返回分类列表数据

     */
     @RequestMapping("/List")
    public String category_list(Model model){
         List<Category> list=categoryservice.categoryList();
         if(list!=null){
            /*
            * 前为前端提取的变量名，后为list对象
            * */
             model.addAttribute("categoryList",list);}
         else {
             model.addAttribute("categoryList",list);
         }
         for (Category c1: list
              ) {
             System.out.println(c1.toString());
         }
         //跳转html
         return"category_list";

    }
  @RequestMapping("/category_add")
  public String category_add(Category category,HttpSession httpSession) {
      if (StringUtils.isEmpty(httpSession.getAttribute("AdminUser"))) {
          System.out.println(ResponseDto.fail("操作失败，先登录", 401, 401).toString());
      } else {
          int flag = categoryservice.addcategory(category);
          if (flag == 0) {
              return "添加失败";
          }else{
              System.out.println(ResponseDto.ok("操作成功").toString());
          }
      }
      return "redirect:/category/List";
  }
//         int flag=categoryservice.addcategory(category);
//         if(flag==0){
//             return "添加失败";
//
//         }
//         //跳转方法
//         return "redirect:/category/List";
//      }

  @RequestMapping("/category_update")
  public String category_update(Integer id,Model model){

            Category category1=categoryservice.findonecategory(id);
            model.addAttribute("category",category1);
            return "category_update";


  }
  @RequestMapping("/show_category_update")
  public String show_category_update(Category category){
         categoryservice.update_category(category);
         return "redirect:/category/List";


  }
}
